package com.itwh.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itwh.project.common.DataGridView;
import com.itwh.project.common.ResultObj;
import com.itwh.project.domain.Course;
import com.itwh.project.domain.Department;
import com.itwh.project.mapper.CourseMapper;
import com.itwh.project.mapper.DepartmentMapper;
import com.itwh.project.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itwh.project.vo.CourseVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 翁煌
 * @since 2020-07-05
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    //可做课程数据显示 查询
    public DataGridView listCourse(List<Long> courseIds, CourseVo courseVo){
        //设置分页条件page和limit
        IPage<Course> page = new Page<>(courseVo.getPage(), courseVo.getLimit());
        //封装查询条件
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",courseIds);
        queryWrapper.eq(courseVo.getDepartmentId()!=null&&courseVo.getDepartmentId()!=0,"department_id",courseVo.getDepartmentId());
        queryWrapper.like(StringUtils.isNotBlank(courseVo.getName()), "name", courseVo.getName());
        queryWrapper.like(StringUtils.isNoneBlank(courseVo.getNumber()),"number",courseVo.getNumber());
        //用自带的selectList（）方法查询使用数据并分页
        IPage<Course> page1 = this.courseMapper.selectPage(page, queryWrapper);
        List<Course> records = page1.getRecords();
        //更换部门id DepartmentId的返回值为部门名称，返回在json数组里
        for (Course course : records) {
            Department department = departmentMapper.selectById(course.getDepartmentId());
            if (department!=null){
                course.setDepartmentName(department.getName());
            }
        }
        return new DataGridView("查询成功",page1.getTotal(),records);
    }

//    //新增
//    @Override
//    public ResultObj saveCourse(CourseVo courseVo) {
//
//        try {
//            //mybatisplus自带的新增方法
//            this.courseMapper.insert(courseVo);
//            return ResultObj.ADD_SUCCESS;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return ResultObj.ADD_ERROR;
//        }
//    }

    //删除这门课在实验室的可做关系
    @Override
    public ResultObj deleteCourse(Long id) {
        try {
            courseMapper.deleteById(id);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

//    //修改
//    @Override
//    public ResultObj updateCourse(CourseVo courseVo) {
//        try {
//            //mybatisplus自带的修改方法
//            courseMapper.updateById(courseVo);
//            return ResultObj.UPDATE_SUCCESS;
//        }catch (Exception e){
//            e.printStackTrace();
//            return ResultObj.UPDATE_ERROR;
//        }
//
//    }


}
